#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @Version : Python-
# @Author  : Shengji He
# @Email   : hsjbit@163.com
# @File    : IslandPerimeter.py
# @Time    : 2020/10/30 9:55
# @Description:
from typing import List


class Solution:
    def islandPerimeter(self, grid: List[List[int]]) -> int:
        """

        :param grid:
        :return:
        """
        if not grid:
            return 0
        m, n = len(grid), len(grid[0])
        ans = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    down = 2 if i < m - 1 and grid[i + 1][j] == 1 else 0
                    right = 2 if j < n - 1 and grid[i][j + 1] == 1 else 0
                    ans += 4 - down - right
        return ans


if __name__ == '__main__':
    S = Solution()
    grid = [[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]
    print(S.islandPerimeter(grid))
    print('done')
